package mysql

import (
	"news/constStatus"
	"news/models"

	"go.uber.org/zap"
)

func CheckGroupExit(groupName string) (err error) {

	sqlStr := "select count(id) from `group` where type = ?"
	var count int
	if err := db.Get(&count, sqlStr, groupName); err != nil {
		zap.L().Error("CheckGroupExit", zap.String("sqlStr:", sqlStr),
			zap.Error(err))
		return constStatus.ErrorNOTTable
	}
	if count > 0 {
		return constStatus.ErrorUserExist
	}
	return
}

// 组的数据入库
func CreateGroupDataSave(data *models.Group) (err error) {
	sqlstr := "insert into `group` (type) values(?)"
	_, err = db.Exec(sqlstr, data.Type)
	if err != nil {
		zap.L().Error("CreateGroupDataSave failed", zap.String("sqlStr:", sqlstr),
			zap.Error(err))
		return err
	}
	return err
}
